from datetime import datetime
from flask import render_template,session,redirect,url_for,flash

from . import main
from .forms import NameForm
from .. import db
from ..models import User

@main.route('/',methods=['GET','POST'])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('你改了你的名字')
        session['name'] = form.name.data
        return redirect(url_for('main.index'))
    return render_template('index.html',form=form,name=session.get('name'),
        current_time=datetime.utcnow())

@main.route('/user/<name>')
def user(name):
    return render_template('user.html',name=name)